//
//  NRWHomeHeaderView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/22.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWHomeHeaderView.h"
#import "SDCycleScrollView.h"
#import "NRWCarCategotyView.h"
#import "NRWHotCarBrandView.h"
#import "NRWHomeChannelView.h"
#import "NRWRotationListModel.h"

@interface NRWHomeHeaderView()<SDCycleScrollViewDelegate, NRWCarCategotyViewDelegate, NRWHotCarBrandViewDelegate, NRWHomeChannelViewDelegate>

@property (strong, nonatomic) SDCycleScrollView *bannerView;

@property (strong, nonatomic) NRWCarCategotyView *categoryView;

@property (strong, nonatomic) NRWHotCarBrandView *hotCarBrandView;

@property (strong, nonatomic) NRWHomeChannelView *channelView;

@end

CGFloat categoryViewH = 55 + 50 + 22;

@implementation NRWHomeHeaderView

- (void)setHotCarModel:(NRWCarBrandModel *)hotCarModel {
    
    _hotCarModel = hotCarModel;
    
    self.hotCarBrandView.hotBrandModel = hotCarModel;
}

- (void)setRotationListModel:(NRWRotationListModel *)rotationListModel {
	
	_rotationListModel = rotationListModel;
	
	NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:rotationListModel.result.count];
	
	[rotationListModel.result enumerateObjectsUsingBlock:^(NRWRotationModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
		[arrM addObject:[NSString stringWithFormat:@"%@%@", imageApi(), obj.rotationImage]];
	}];
	
	self.bannerView.imageURLStringsGroup = arrM.copy;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH(), getAdaptationWidth(160)) delegate:self placeholderImage:getImage(@"default_banner")];
    self.bannerView.infiniteLoop = YES;
	self.bannerView.autoScroll = YES;
	self.bannerView.autoScrollTimeInterval = 3;
	self.bannerView.delegate = self;
    [self addSubview:self.bannerView];
    
    self.categoryView = [[NRWCarCategotyView alloc] init];
    self.categoryView.frame = CGRectMake(0, self.bannerView.viewMaxY + 25, SCREEN_WIDTH(), categoryViewH);
    self.categoryView.delegate = self;
    [self addSubview:self.categoryView];
    
    self.hotCarBrandView = [[NRWHotCarBrandView alloc] init];
    self.hotCarBrandView.frame = CGRectMake(0, self.categoryView.viewMaxY + 20, SCREEN_WIDTH(), 90);
    self.hotCarBrandView.delegate = self;
    [self addSubview:self.hotCarBrandView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = getColor(@"#F7F7F7");
    lineView.frame = CGRectMake(0, self.hotCarBrandView.viewMaxY + 10, SCREEN_WIDTH(), 5);
    self.addSubview(lineView);
    
    self.channelView = [[NRWHomeChannelView alloc] init];
    self.channelView.delegate = self;
    self.channelView.frame = CGRectMake(0, lineView.viewMaxY, SCREEN_WIDTH(), 40);
    [self addSubview:self.channelView];
}

- (void)viewWillAppear {
    
    [self.bannerView adjustWhenControllerViewWillAppera];
}

#pragma mark - brannerDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
	
	if ([self.delegate respondsToSelector:@selector(homeBannerView:bannerDidClickAtIndex:)]) {
		[self.delegate homeBannerView:self bannerDidClickAtIndex:index];
	}
}

#pragma mark - NRWCarCategotyViewDelegate, NRWHotCarBrandViewDelegate
- (void)carCategotyView:(NRWCarCategotyView *)view itemDidClickWithTag:(CarCategotyViewClick)tag {
    
    if ([self.delegate respondsToSelector:@selector(homeHeaderView:carCategoryDidClickWithTag:)]) {
        
        [self.delegate homeHeaderView:self carCategoryDidClickWithTag:tag];
    }
}

- (void)hotCarBrandView:(NRWHotCarBrandView *)view didClickAtIndex:(NSInteger)index {
    
    if ([self.delegate respondsToSelector:@selector(homeHeaderView:hotCarBrandDidClickAtIndex:)]) {
        
        [self.delegate homeHeaderView:self hotCarBrandDidClickAtIndex:index];
    }
}

- (void)homeChannelView:(NRWHomeChannelView *)view didSelectedWithIndex:(NSInteger)index {
    
    if ([self.delegate respondsToSelector:@selector(homeHeaderView:channelDidClickAtIndex:)]) {
        
        [self.delegate homeHeaderView:self channelDidClickAtIndex:index];
    }
}

@end
